package com.controller;

import com.biz.*;
import com.entity.*;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/orders")
public class OrdersAction {

    @Autowired
    private IOrdersBiz biz;

    @Autowired
    private IAddressBiz addressbiz;

    @Autowired
    private IShopcarBiz shopcarBiz;

    @Autowired
    private IBookBiz bookBiz;

    @Autowired
    private IOrder_detailBiz orderDetailBiz;

    @Autowired
    private IDiscussBiz discussBiz;

    //列表
    @RequestMapping("/list")
    public String getAll(HttpSession session)
    {
        List<Orders> list=biz.getAll();
        session.setAttribute("olist",list);
        return "/back/orders_list";
    }
/*
    //前端列表
    @RequestMapping("/list2")
    public String selectAll(HttpSession session)
    {
        List<Orders> list=biz.getAll();
        session.setAttribute("olist",list);
        return "orderlist";
    }*/

    //根据状态获取列表
    @RequestMapping("/getAllBystate")
    public String getAllBystate(HttpSession session)
    {
        List<Orders> list=biz.getAllBystate();
        session.setAttribute("state2list",list);
        return "/back/orders_list";
    }


    //增加
    @RequestMapping("/add")
    public String add(Orders o,@RequestParam(required = false,value = "id") Integer[] ids,HttpSession session)
    {
        Userinfo us= (Userinfo) session.getAttribute("loger");
        o.setUid(us);
        biz.add_Orders(o);
        for(Integer id:ids){
            //从购物车里获取下单信息
            Shopcar shopcar=shopcarBiz.getById(id);
            addDetail(shopcar);
        }
        return "redirect:list.action";
    }


    //删除
    @RequestMapping("/del")
    public String del(int id)
    {
        biz.del_Orders(id);
        return "redirect:list.action";
    }


    //根据id
    @RequestMapping("/getbyid")
    public ModelAndView getById(int id)
    {
       Orders o=biz.getById(id);
       ModelAndView mv=new ModelAndView();
       mv.addObject("oo",o);
       mv.setViewName("/back/orders_mod");
       return mv;
    }


    //修改
    @RequestMapping("/mod")
    public String mod(Orders o)
    {
        biz.mod_Orders(o);
        return "redirect:list.action";
    }

    //后台修改订单状态（即发货）
    @RequestMapping("/send")
    public String send(Integer id)
    {
        Orders o=biz.getById(id);
        o.setState(3);
        biz.mod_Status(o);
        return "redirect:getAllBystate.action";
    }

    //根据用户id获取一组订单列表
    @RequestMapping("/getlistByuid")
    public String getByUid(HttpSession sess) {
        Userinfo userinfo=(Userinfo)sess.getAttribute("loger");
        List<Map> map=  biz.getByUid(userinfo.getId());
        sess.setAttribute("orderlist",map);
       return  "orderlist";
    }

    //根据oid+uid  获取订单详情
    @RequestMapping("/getByIdAndUid")
    public String getByIdAndUid(Integer id,HttpSession session){
    Userinfo userinfo=(Userinfo)session.getAttribute("loger");
    Order_detail orderDetail=orderDetailBiz.getByIdAndUid(id, userinfo.getId());
    session.setAttribute("od",orderDetail);
    return "orderdetail";
    }

    @RequestMapping("/getByIdAndUid2")
    public String getByIdAndUid2(Integer id,HttpSession session){
        Userinfo userinfo=(Userinfo)session.getAttribute("loger");
        Order_detail orderDetail=orderDetailBiz.getByIdAndUid(id, userinfo.getId());
        session.setAttribute("od",orderDetail);
        return "discuss_page";
    }

    /**
     * 确认收货（修改订单状态） 待收货：4  改为  已收货：5
     * 订单状态 0待付款，1已付款，2待发货，3.已发货,4待收货，5已收货，6待评价，7已完成
     * */
    //根据oid  确认收货
    @RequestMapping("/isOk")
    public String isOk(Integer id,HttpSession session){
        Userinfo userinfo=(Userinfo)session.getAttribute("loger");
        //根据oid  获取要修改的订单实体对象
        Orders orders=biz.getById(id);
        orders.setState(5);
        //修改订单状态
        biz.mod_Status(orders);
        return "redirect:getlistByuid.action";
    }

    /**
     * 增加书籍评价  并进行订单状态修改 6:待评价  改为  7:已完成
     * */
    //根据bookid  进行书籍评价
    @RequestMapping("/discuss")
    public String discuss(Integer oid,Discuss discuss,HttpSession session){
        System.out.println("*********书籍id"+discuss.getBookid());
       Userinfo userinfo=(Userinfo)session.getAttribute("loger");

        /*discuss.setUsername(userinfo);
         Books books=bookBiz.getById(bookid);//获取书籍实体
        discuss.setBookid(books);*/
        discussBiz.add_discuss(discuss);//增加评论
        Orders orders=biz.getById(oid);
        orders.setState(7);
        biz.mod_Status(orders);//修改订单状态
        return "redirect:getlistByuid.action";
    }

    //从购物车去下单
    public String  fromCarToBuy(@RequestParam(required = false,value = "id") Integer[] id,HttpSession session){
        Userinfo userinfo=(Userinfo) session.getAttribute("loger");
        List<Address> addreddList=addressbiz.getListByUid(userinfo.getId());
        session.setAttribute("addreddList",addreddList);
        //根据一组购物车id获取下单信息
        List<Map> shopcarList=shopcarBiz.getListByIds(id);
        session.setAttribute("shopcarList",shopcarList);
        return  "confirmOrder";
    }


    /**
     * 把从购物车里获取的信息放到订单详情
     */
    public void  addDetail(Shopcar shopcar){
        Order_detail detail=new Order_detail();
        Books books=bookBiz.getById(shopcar.getBookid());
        detail.setBookid(books);
        detail.setBookprice(shopcar.getPrice());
        detail.setNum(shopcar.getNum());
       /* detail.setOrderid(biz.getMaxOid());*/
        Orders orders=new Orders();
        orders.setId(biz.getMaxOid());
        detail.setOrderid(orders);
        orderDetailBiz.add_order_detail(detail);
    }

}
